# Check yaml installation
FILE(GLOB_RECURSE SUNDIALS_FOUND_CMAKE_FILE "${SUNDIALS_INSTALL_PATH}/SUNDIALSConfig.cmake")

IF (SUNDIALS_FOUND_CMAKE_FILE)
  INCLUDE(${SUNDIALS_FOUND_CMAKE_FILE})

  #GET_TARGET_PROPERTY(TINES_SUNDIALS_COMPILE_OPTIONS "SUNDIALS::generic_static" INTERFACE_COMPILE_OPTIONS)
  GET_TARGET_PROPERTY(TINES_SUNDIALS_INCLUDE_PATH "SUNDIALS::generic_static" INTERFACE_INCLUDE_DIRECTORIES)
  GET_TARGET_PROPERTY(TINES_SUNDIALS_LIBRARIES_IMPORTED_CONFIGURATION "SUNDIALS::generic_static" IMPORTED_CONFIGURATIONS)
  SET(TINES_SUNDIALS_LIBRARIES_PROPERTY_NAME "IMPORTED_LOCATION_${TINES_SUNDIALS_LIBRARIES_IMPORTED_CONFIGURATION}")

  GET_TARGET_PROPERTY(TINES_SUNDILAS_LIBRARIES_CVODE "SUNDIALS::cvode_static" ${TINES_SUNDIALS_LIBRARIES_PROPERTY_NAME})
  GET_TARGET_PROPERTY(TINES_SUNDILAS_LIBRARIES_NVECSERIAL "SUNDIALS::nvecserial_static" ${TINES_SUNDIALS_LIBRARIES_PROPERTY_NAME})
  
  MESSAGE(STATUS "  SUNDIALS include dir ${TINES_SUNDIALS_INCLUDE_PATH}")
  MESSAGE(STATUS "  SUNDIALS libriaries")
  MESSAGE(STATUS "     ${TINES_SUNDILAS_LIBRARIES_CVODE}")
  MESSAGE(STATUS "     ${TINES_SUNDILAS_LIBRARIES_NVECSERIAL}")
  #MESSAGE(STATUS "  COMPILE option ${TINES_SUNDIALS_COMPILE_OPTIONS})
  SET(SUNDIALS_FOUND ON)
  MESSAGE("-- SUNDIALS is found at ${SUNDIALS_INSTALL_PATH}")  
  
  ADD_LIBRARY(sundials INTERFACE)
  SET_TARGET_PROPERTIES(sundials PROPERTIES 
    INTERFACE_INCLUDE_DIRECTORIES "${TINES_SUNDIALS_INCLUDE_PATH}"
    INTERFACE_COMPILE_OPTIONS "-I${TINES_SUNDIALS_INCLUDE_PATH}"
    INTERFACE_LINK_LIBRARIES "${TINES_SUNDILAS_LIBRARIES_CVODE};${TINES_SUNDILAS_LIBRARIES_NVECSERIAL}"
  )    
  INSTALL(TARGETS sundials
    EXPORT tines-targets
    LIBRARY DESTINATION "${TINES_INSTALL_LIB_PATH}"
    ARCHIVE DESTINATION "${TINES_INSTALL_LIB_PATH}"
  )
  # INCLUDE(${SUNDIALS_FOUND_CMAKE_FILE})
  # SET(SUNDIALS_FOUND ON)
  # MESSAGE("-- SUNDIALS is found at ${SUNDIALS_INSTALL_PATH}")  
ELSE()
  MESSAGE(FATAL_ERROR "-- SUNDIALS is not found at ${SUNDIALS_INSTALL_PATH}")
ENDIF()
